package jenkins

import (
	"github.com/gin-gonic/gin"
	"github.com/wonderivan/logger"
	"k8s-platform/model"
	"k8s-platform/service/jenkins"
	"net/http"
)

// 获取Node列表，支持过滤、排序、分页//
var Jenkinsjob jenkinsjob

type jenkinsjob struct{}

//func (*credential) Add(c *gin.Context) {
//	p := &model.Credential{}
//	if err := c.ShouldBindJSON(p); err != nil {
//		c.JSON(http.StatusOK, gin.H{
//			"code": 90400,
//			"msg":  err.Error(),
//			"data": nil,
//		})
//		return
//	}
//	if err := cmdb.Credential.Add(p); err != nil {
//		c.JSON(http.StatusOK, gin.H{
//			"code": 90500,
//			"msg":  err.Error(),
//			"data": nil,
//		})
//		return
//	}
//	c.JSON(http.StatusOK, gin.H{
//		"code": 0,
//		"msg":  "创建credential成功",
//		"data": nil,
//	})
//}

func (*jenkinsjob) Create(ctx *gin.Context) {

	params := &model.Jenkinsjoblist{}
	if err := ctx.Bind(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	data, err := service.Jenkinsjob.Create(params)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "创建  " + params.JobName + "  jobs成功",
		"data": data,
	})
}

func (*jenkins) Getjobconfig(ctx *gin.Context) {
	params := new(struct {
		JobName string `form:"jobname"`
		Page    int    `form:"page"`
		Limit   int    `form:"limit"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	data, err := service.Jenkinsjob.GetConfig(params.JobName)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": data,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取job配置成功",
		"data": data,
	})
}

func (*jenkins) UpdateConfig(ctx *gin.Context) {
	params := new(struct {
		JobName     string `form:"jobname"`
		GitlistName string `json:"gitlistname"`
		//GitlistGroup      string `json:"gitlistgroup"`
		GitlistUrl string `json:"gitlisturl"`
		Branche    string `form:"branche"`
		//SelectedEnv       string `form:"selectedEnv"`
		ImagesRegion      string `form:"imagesregion"`
		JenkinslistCIdata string `form:"jenkinslistCIdata"`
		CIcommand         string `form:"CIcommand"`
		ServerlistCDdata  string `form:"serverlistCDdata"`
		CDcommand         string `form:"CDcommand"`
		Piplelinedata     string `form:"piplelinedata"`
		//Jobid             int64  `form:"jobid"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	data, err := service.Jenkinsjob.UpdateConfig(params.JobName, params.JenkinslistCIdata, params.GitlistName, params.GitlistUrl, params.Branche, params.CIcommand, params.CDcommand, params.Piplelinedata, params.ImagesRegion)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": data,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取job更新配置成功",
		"data": data,
	})
}

func (*jenkins) Deletejob(ctx *gin.Context) {
	params := new(struct {
		JobName string `form:"jobname"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	data, err := service.Jenkinsjob.Deletejob(params.JobName)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": data,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  data,
		"data": data,
	})
}

func (*jenkins) GetYAML(ctx *gin.Context) {
	data, err := service.Jenkinsjob.GetYAML()
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": data,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  200,
		"data": data,
	})
}
